<template>
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import { reqImportEmployee } from '@/api/employees'
// import router from '@/router'
export default {
  name: 'Import',
  computed: {
    type() {
      return this.$route.query.type
    }
  },
  methods: {
    formatDate(numb, format) {
      const time = new Date((numb) * 24 * 3600000 + 1) // 毫秒
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    },
    async handleSuccess({ header, results }) {
    //   console.log(this.formatDate(1, '-'))
      //   console.log(header, results)
      if (this.type === 'user') {
        const userRelations = {
          '入职日期': 'timeOfEntry',
          '手机号': 'mobile',
          '姓名': 'username',
          '转正日期': 'correctionTime',
          '工号': 'workNumber'
        }
        const data = [] // 我们最终组装的对象数组数据
        results.forEach(item => {
          const userInfo = { }
          // 处理userInfo的属性
          // console.log(item)
          for (const key in item) {
            //   console.log(key, item, item[key])
            if (['timeOfEntry', 'correctionTime'].includes(userRelations[key])) {
              userInfo[userRelations[key]] = this.formatDate(item[key], '-')
            } else {
              userInfo[userRelations[key]] = item[key]
            }
            //   const englishKey = userRelations[key]
            // console.log(englishKey, item[key])
            //   将中文转成英文
            //   userInfo[englishKey] = item[key]
          }
          data.push(userInfo)
        })
        //   //   console.log(data)

        //   // 调用接口，导入员工模板
        await reqImportEmployee(data)
      }
      // 回退到员工页面
      //   router.back()
      this.$router.back()
    }
  }

}
</script>

<style>

</style>
